package com.example.innovate.controller;

import com.example.innovate.bean.Result;
import com.example.innovate.bean.SignRateYear;
import com.example.innovate.bean.SignedStatus;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.SigningRateDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.SigningRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/api/signingRate")
@Api(tags = "签约统计接口")
public class SigningRateController {

    @Autowired
    SigningRateService signingRateService;
    @Autowired
    AdminService adminService;

    @PostMapping("/addSigningRate")
    @ApiOperation("添加签约信息")
    public Result addSigningRate(@RequestBody SigningRateDO signingRateDO) throws Exception{
        signingRateService.addInfo(signingRateDO);
        return new Result(200,"success","添加成功",signingRateDO);
    }

    @DeleteMapping("/deleteSigningRate")
    @ApiOperation("删除签约信息")
    public Result deleteSigningRate(@RequestParam("id") int id,
                                    @RequestParam("adminUserName") String adminUserName) throws Exception{
        SigningRateDO signingRateDO = signingRateService.findById(id);
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(signingRateDO!=null){
            if(adminInfoDO!=null){
                if(adminInfoDO.getAdminIsUpdate().equals(false)){
                    return new Result(400,"error","没有删除权限",signingRateDO);
                }
            }
            signingRateService.deleteInfo(id);
            return new Result(200,"success","删除成功",signingRateDO);
        }
        else {
            return new Result(400,"error","信息不存在",signingRateDO);
        }

    }

    @PostMapping("/updateSigningRate")
    @ApiOperation("修改签约信息")
    public Result updateSigningRate(@RequestBody SigningRateDO signingRateDO,
                                    @RequestParam("adminUserName") String adminUserName) throws Exception{
        SigningRateDO signingRateDO1 = signingRateService.findById(signingRateDO.getSiId());
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(signingRateDO1!=null){
            if(adminInfoDO!=null){
                if(adminInfoDO.getAdminIsUpdate().equals(false)){
                    return new Result(400,"error","没有修改权限",signingRateDO);
                }
            }
            signingRateService.updateInfo(signingRateDO);
            return new Result(200,"success","修改成功",signingRateDO);
        }
        else {
            return new Result(400,"error","信息不存在",signingRateDO);
        }
    }

    @GetMapping("/findSigningRate")
    @ApiOperation("查找签约信息")
    public Result findAllSigningRate(@RequestParam("pageNumber") Integer pageNumber,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("className") String className) throws Exception{
        if(className.equals("superAdmin")){
            className = null;
            List<SigningRateDO> list = signingRateService.findInfo(className,pageNumber,pageSize);
            return new Result(200,"success","查找成功",list);
        }
        else{
            List<SigningRateDO> list = signingRateService.findInfo(className,pageNumber,pageSize);
            return new Result(200,"success","查找成功",list);
        }

    }

    @GetMapping("/chartSign")
    @ApiOperation("图表统计-签约情况占比")
    public Result findSignedStatus(String className) throws Exception{
        List<SignedStatus> list = new LinkedList<>();
        SignedStatus signedStatus = signingRateService.findSign(className,"协议就业");
        list.add(signedStatus);
        SignedStatus signedStatus1 = signingRateService.findSign(className,"自主创业");
        list.add(signedStatus1);
        SignedStatus signedStatus2 = signingRateService.findSign(className,"基层项目");
        list.add(signedStatus2);
        SignedStatus signedStatus3 = signingRateService.findSign(className,"科研助理");
        list.add(signedStatus3);
        return new Result(200,"success","查找成功",list);
    }

    @GetMapping("/chartYear")
    @ApiOperation("图表统计-年度同比变化")
    public Result findInfoByYear(Integer year) throws Exception{
        if(year==null){
            Calendar cal = Calendar.getInstance();
            year = cal.get(Calendar.YEAR);
        }
        List<SignRateYear> list = new LinkedList<>();
        //未就业、签约、非失业、灵活就业
        for (int i = year;i>year-3;i--){
            String year1 = i+"";
            SignRateYear signRateYear = signingRateService.findByYear(year1,"未就业");
            list.add(signRateYear);
        }
        for (int i = year;i>year-3;i--){
            String year1 = i+"";
            SignRateYear signRateYear = signingRateService.findByYear(year1,"签约");
            list.add(signRateYear);
        }
        for (int i = year;i>year-3;i--){
            String year1 = i+"";
            SignRateYear signRateYear = signingRateService.findByYear(year1,"非失业");
            list.add(signRateYear);
        }
        for (int i = year;i>year-3;i--){
            String year1 = i+"";
            SignRateYear signRateYear = signingRateService.findByYear(year1,"灵活就业");
            list.add(signRateYear);
        }
        return new Result(200,"success","查找成功",list);
    }
}
